package stego.StegoCaller;

import java.io.*;
import java.util.ArrayList;
import javax.swing.JTextPane;
import stego.Drivers;

public class Caller_Encryption_XOR
{

    static String strMyClassName = "Caller_Encryption_XOR";

    public Caller_Encryption_XOR()
    {
        try
        {
            Drivers.jop_Error("You should not see this message!", "Programmer Error!");
        }
        catch(Exception e)
        {
            Drivers.eop("Caller_Encryption_XOR Constructor", strMyClassName, e, e.getLocalizedMessage(), true);
        }
    }

    public static File encryptFile(File sourceFile, File encryptionJAR, File generatedEncryptionKey, File encryptionSaveDirectory, JTextPane jtxtpneOut)
    {
        String pathToNewEncryptedFile;
        File fleExpectedFile;
        String strSourceFileName = sourceFile.getCanonicalPath();
        strSourceFileName = strSourceFileName.substring(strSourceFileName.lastIndexOf(System.getProperty("file.separator")) + 1);
        String extension = "";
        try
        {
            extension = strSourceFileName.substring(strSourceFileName.lastIndexOf(".1"));
        }
        catch(Exception e)
        {
            extension = "txt";
        }
        String strEncryptedFileName = (new StringBuilder(String.valueOf(strSourceFileName.substring(0, strSourceFileName.lastIndexOf("."))))).append("_ENCRYPTED").append(".").append(extension).toString();
        pathToNewEncryptedFile = (new StringBuilder(String.valueOf(encryptionSaveDirectory.getCanonicalPath()))).append(System.getProperty("file.separator")).append(strEncryptedFileName).toString();
        String strJarName = encryptionJAR.getCanonicalPath().toString().substring(encryptionJAR.getCanonicalPath().toString().lastIndexOf(System.getProperty("file.separator")) + 1);
        String strCommand = (new StringBuilder("java -jar \"")).append(encryptionJAR.getCanonicalPath()).append("\"").append(" -e ").append("\"").append(sourceFile.getCanonicalPath()).append("\"").append(" ").append("\"").append(generatedEncryptionKey).append("\"").append(" ").append("\"").append(pathToNewEncryptedFile).append("\"").toString();
        Drivers.appendTextToJTextPane(jtxtpneOut, (new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
        Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
        Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        for(String line = ""; (line = input.readLine()) != null;)
        {
            Drivers.sop(line);
            Drivers.appendTextToJTextPane(jtxtpneOut, line, true);
            Drivers.sop(line);
        }

        Drivers.sop("\n\n");
        Drivers.appendTextToJTextPane(jtxtpneOut, "\n\n", true);
        ArrayList alFile = Drivers.getFilesUnderSelectedDirectory(encryptionSaveDirectory, new String[] {
            extension
        });
        fleExpectedFile = new File(pathToNewEncryptedFile);
        if(alFile == null || !alFile.contains(fleExpectedFile))
        {
            break MISSING_BLOCK_LABEL_482;
        }
        Drivers.sop((new StringBuilder("Encrypted File Write Successful at: ")).append(fleExpectedFile.getCanonicalPath()).toString());
        return fleExpectedFile;
        try
        {
            Drivers.sop((new StringBuilder("Encrypted File Write FAILED at: ")).append(pathToNewEncryptedFile).toString());
        }
        catch(Exception e)
        {
            Drivers.eop("encryptFile", strMyClassName, e, e.getLocalizedMessage(), true);
            return null;
        }
        return null;
    }

    public static File generateEncryptionKey(File fleEncryptionJAR, int keySize, File outputKeyPath, String keyName, String keyExtensionWithoutTheDot, JTextPane jtxtpneOut)
    {
        String strEncryptionKeyPathAndName;
        File fleExpectedFile;
        String strJarName = fleEncryptionJAR.getCanonicalPath().toString().substring(fleEncryptionJAR.getCanonicalPath().toString().lastIndexOf(System.getProperty("file.separator")) + 1);
        strEncryptionKeyPathAndName = (new StringBuilder(String.valueOf(outputKeyPath.getCanonicalPath()))).append(System.getProperty("file.separator")).append(keyName).append(".").append(keyExtensionWithoutTheDot).toString();
        String strCommand = (new StringBuilder("java -jar \"")).append(fleEncryptionJAR.getCanonicalPath()).append("\"").append(" -g ").append(keySize).append(" \"").append(strEncryptionKeyPathAndName).append("\"").toString();
        Drivers.appendTextToJTextPane(jtxtpneOut, (new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
        Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
        Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        for(String line = ""; (line = input.readLine()) != null;)
        {
            Drivers.sop(line);
            Drivers.appendTextToJTextPane(jtxtpneOut, line, true);
            Drivers.sop(line);
        }

        Drivers.sop("\n\n");
        Drivers.appendTextToJTextPane(jtxtpneOut, "\n\n", true);
        ArrayList alFile = Drivers.getFilesUnderSelectedDirectory(outputKeyPath, new String[] {
            keyExtensionWithoutTheDot
        });
        fleExpectedFile = new File(strEncryptionKeyPathAndName);
        if(alFile == null || !alFile.contains(fleExpectedFile))
        {
            break MISSING_BLOCK_LABEL_359;
        }
        Drivers.sop((new StringBuilder("Encryption Key File Write Successful at: ")).append(fleExpectedFile.getCanonicalPath()).toString());
        return fleExpectedFile;
        try
        {
            Drivers.sop((new StringBuilder("Encryption Key File Write FAILED at: ")).append(strEncryptionKeyPathAndName).toString());
        }
        catch(Exception e)
        {
            Drivers.eop("generateEncryptionKey", strMyClassName, e, e.getLocalizedMessage(), true);
            return null;
        }
        return null;
    }

    public static String decryptText(File encryptionJAR, String stringRepresentationOfByteArray_messageToDecrypt, String stringRepresentationOfByteArray_encryptionKey)
    {
        String line;
        String allOutput;
        line = "";
        allOutput = "";
        String strJarName = encryptionJAR.getCanonicalPath().toString().substring(encryptionJAR.getCanonicalPath().toString().lastIndexOf(System.getProperty("file.separator")) + 1);
        String strCommand = (new StringBuilder("java -jar \"")).append(encryptionJAR.getCanonicalPath()).append("\"").append(" -dS ").append("\"").append(stringRepresentationOfByteArray_messageToDecrypt).append("\"").append(" ").append("\"").append(stringRepresentationOfByteArray_encryptionKey).toString();
        Drivers.appendTextToInteractionContent_TextPane((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
        Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
        Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        line = "";
        for(allOutput = ""; (line = input.readLine()) != null; allOutput = (new StringBuilder(String.valueOf(allOutput))).append(line).toString())
        {
            Drivers.sop(line);
            Drivers.appendTextToInteractionContent_TextPane(line, true);
            Drivers.sop(line);
        }

        Drivers.sop((new StringBuilder("Returning full line received from stdIn: ")).append(allOutput).toString());
        Drivers.appendTextToInteractionContent_TextPane((new StringBuilder("Returning full line received from stdIn: ")).append(allOutput).toString(), true);
        Drivers.sop("\n\n");
        Drivers.appendTextToInteractionContent_TextPane("\n\n", true);
        return allOutput;
        Exception e;
        e;
        Drivers.eop("decryptText", strMyClassName, e, e.getLocalizedMessage(), true);
        return line;
    }

    public static File decryptFile(File sourceFile, File encryptionJAR, File generatedEncryptionKey, File encryptionSaveDirectory, JTextPane jtxtpneOut)
    {
        String pathToNewEncryptedFile;
        File fleExpectedFile;
        String strSourceFileName = sourceFile.getCanonicalPath();
        strSourceFileName = strSourceFileName.substring(strSourceFileName.lastIndexOf(System.getProperty("file.separator")) + 1);
        String extension = "";
        try
        {
            extension = strSourceFileName.substring(strSourceFileName.lastIndexOf(".1"));
        }
        catch(Exception e)
        {
            extension = "txt";
        }
        String strEncryptedFileName = (new StringBuilder(String.valueOf(strSourceFileName.substring(0, strSourceFileName.lastIndexOf("."))))).append("_DECRYPTED").append(".").append(extension).toString();
        pathToNewEncryptedFile = (new StringBuilder(String.valueOf(encryptionSaveDirectory.getCanonicalPath()))).append(System.getProperty("file.separator")).append(strEncryptedFileName).toString();
        String strJarName = encryptionJAR.getCanonicalPath().toString().substring(encryptionJAR.getCanonicalPath().toString().lastIndexOf(System.getProperty("file.separator")) + 1);
        String strCommand = (new StringBuilder("java -jar \"")).append(encryptionJAR.getCanonicalPath()).append("\"").append(" -d ").append("\"").append(sourceFile.getCanonicalPath()).append("\"").append(" ").append("\"").append(generatedEncryptionKey).append("\"").append(" ").append("\"").append(pathToNewEncryptedFile).append("\"").toString();
        Drivers.appendTextToJTextPane(jtxtpneOut, (new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
        Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
        Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        for(String line = ""; (line = input.readLine()) != null;)
        {
            Drivers.sop(line);
            Drivers.appendTextToJTextPane(jtxtpneOut, line, true);
            Drivers.sop(line);
        }

        Drivers.sop("\n\n");
        Drivers.appendTextToJTextPane(jtxtpneOut, "\n\n", true);
        ArrayList alFile = Drivers.getFilesUnderSelectedDirectory(encryptionSaveDirectory, new String[] {
            extension
        });
        fleExpectedFile = new File(pathToNewEncryptedFile);
        if(alFile == null || !alFile.contains(fleExpectedFile))
        {
            break MISSING_BLOCK_LABEL_482;
        }
        Drivers.sop((new StringBuilder("Decrypted File Write Successful at: ")).append(fleExpectedFile.getCanonicalPath()).toString());
        return fleExpectedFile;
        try
        {
            Drivers.sop((new StringBuilder("Decrypted File Write FAILED at: ")).append(pathToNewEncryptedFile).toString());
        }
        catch(Exception e)
        {
            Drivers.eop("decryptFile", strMyClassName, e, e.getLocalizedMessage(), true);
            return null;
        }
        return null;
    }

    public static String encryptText(File encryptionJAR, String stringRepresentationOfByteArray_messageToEncrypt, String stringRepresentationOfByteArray_encryptionKey)
    {
        String line;
        String allOutput;
        line = "";
        allOutput = "";
        String strJarName = encryptionJAR.getCanonicalPath().toString().substring(encryptionJAR.getCanonicalPath().toString().lastIndexOf(System.getProperty("file.separator")) + 1);
        String strCommand = (new StringBuilder("java -jar \"")).append(encryptionJAR.getCanonicalPath()).append("\"").append(" -eS ").append("\"").append(stringRepresentationOfByteArray_messageToEncrypt).append("\"").append(" ").append("\"").append(stringRepresentationOfByteArray_encryptionKey).toString();
        Drivers.appendTextToInteractionContent_TextPane((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString(), true);
        Drivers.sop((new StringBuilder("Executing the following command: \"")).append(strCommand).append("\"").toString());
        Process p = Runtime.getRuntime().exec((new StringBuilder("cmd /c ")).append(strCommand).toString());
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedWriter output = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
        line = "";
        for(allOutput = ""; (line = input.readLine()) != null; allOutput = (new StringBuilder(String.valueOf(allOutput))).append(line).toString())
        {
            Drivers.sop(line);
            Drivers.appendTextToInteractionContent_TextPane(line, true);
            Drivers.sop(line);
        }

        Drivers.sop((new StringBuilder("Returning full line received from stdIn: ")).append(allOutput).toString());
        Drivers.appendTextToInteractionContent_TextPane((new StringBuilder("Returning full line received from stdIn: ")).append(allOutput).toString(), true);
        Drivers.sop("\n\n");
        Drivers.appendTextToInteractionContent_TextPane("\n\n", true);
        return allOutput;
        Exception e;
        e;
        Drivers.eop("encryptText", strMyClassName, e, e.getLocalizedMessage(), true);
        return line;
    }

}
